package com.yubest.junit.mail;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Smtp {

    final String smtpHost;
    final String username;
    final String password;

    public Smtp(String smtpHost, String username, String password) {
        this.smtpHost = smtpHost;
        this.username = username;
        this.password = password;
    }

    public void send(String toUser, String topic, String context) {
        Session session = this.createSSLSession();
        Message message = null;
        try {
            message = createTextMessage(session, this.username, toUser, topic, context);
            Transport.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    private Session createSSLSession() {
        Properties props = new Properties();
        props.put("mail.smtp.host", this.smtpHost); // SMTP主机名
        props.put("mail.smtp.port", "465"); // 主机端口号
        props.put("mail.smtp.auth", "true"); // 是否需要用户认证
        // 启动SSL:
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.port", "465");
        // 发送邮件阻塞问题解决——设置 smtp 超时时间
        // 设置接收超时时间，10秒
        props.put("mail.smtp.connectiontimeout", 10000);
        // 设置读取超时时间，10秒
        props.put("mail.smtp.timeout", 10000);
        // 设置写入超时时间，10秒
        props.put("mail.smtp.writetimeout", 10000);
        Session session = Session.getInstance(props, new Authenticator() {
            // 用户名+口令认证:
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(Smtp.this.username, Smtp.this.password);
            }
        });
        return session;
    }

    private Session createTLSSession() {
        Properties props = new Properties();
        props.put("mail.smtp.host", this.smtpHost); // SMTP主机名
        props.put("mail.smtp.port", "587"); // 主机端口号
        props.put("mail.smtp.auth", "true"); // 是否需要用户认证
        props.put("mail.smtp.starttls.enable", "true"); // 启用TLS加密
        Session session = Session.getInstance(props, new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(Smtp.this.username, Smtp.this.password);
            }
        });
        return session;
    }

    private Session createInsecureSession(String host, String username, String password) {
        Properties props = new Properties();
        props.put("mail.smtp.host", this.smtpHost); // SMTP主机名
        props.put("mail.smtp.port", "25"); // 主机端口号
        props.put("mail.smtp.auth", "true"); // 是否需要用户认证
        Session session = Session.getInstance(props, new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(Smtp.this.username, Smtp.this.password);
            }
        });
        return session;
    }

    private Message createTextMessage(Session session, String from, String to, String subject, String body) throws MessagingException {
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
        message.setSubject(subject, "UTF-8");
        message.setText(body, "UTF-8");
        return message;
    }

}
